defmodule Giom.Accounting do
  @moduledoc """
  The Accounting context.
  """

  import Ecto.Query, warn: false
  alias Giom.Repo

  alias Giom.Accounting.Stock

  @doc """
  Returns the list of stocks.

  ## Examples

      iex> list_stocks()
      [%Stock{}, ...]

  """
  def list_stocks do
    Repo.all(Stock)
  end

  @doc """
  Gets a single stock.

  Raises `Ecto.NoResultsError` if the Stock does not exist.

  ## Examples

      iex> get_stock!(123)
      %Stock{}

      iex> get_stock!(456)
      ** (Ecto.NoResultsError)

  """
  def get_stock!(id), do: Repo.get!(Stock, id)

  @doc """
  Creates a stock.

  ## Examples

      iex> create_stock(%{field: value})
      {:ok, %Stock{}}

      iex> create_stock(%{field: bad_value})
      {:error, %Ecto.Changeset{}}

  """
  def create_stock(attrs \\ %{}) do
    %Stock{}
    |> Stock.changeset(attrs)
    |> Repo.insert()
  end

  @doc """
  Updates a stock.

  ## Examples

      iex> update_stock(stock, %{field: new_value})
      {:ok, %Stock{}}

      iex> update_stock(stock, %{field: bad_value})
      {:error, %Ecto.Changeset{}}

  """
  def update_stock(%Stock{} = stock, attrs) do
    stock
    |> Stock.changeset(attrs)
    |> Repo.update()
  end

  @doc """
  Deletes a Stock.

  ## Examples

      iex> delete_stock(stock)
      {:ok, %Stock{}}

      iex> delete_stock(stock)
      {:error, %Ecto.Changeset{}}

  """
  def delete_stock(%Stock{} = stock) do
    Repo.delete(stock)
  end

  @doc """
  Returns an `%Ecto.Changeset{}` for tracking stock changes.

  ## Examples

      iex> change_stock(stock)
      %Ecto.Changeset{source: %Stock{}}

  """
  def change_stock(%Stock{} = stock) do
    Stock.changeset(stock, %{})
  end

  alias Giom.Accounting.Ist

  @doc """
  Returns the list of ists.

  ## Examples

      iex> list_ists()
      [%Ist{}, ...]

  """
  def list_ists do
   # burda tutar değerini vt'de tutmuyoruz sorguda fiyat ve miktar dan çarpıp güncelliyoruz.
   # amount-tutar sahası sanal saha.
   (from stk in Ist, select: %{stk | amount:  fragment("? * ?",stk.price,stk.qty)}) |> Repo.all
  end

  @doc """
  Gets a single ist.

  Raises `Ecto.NoResultsError` if the Ist does not exist.

  ## Examples

      iex> get_ist!(123)
      %Ist{}

      iex> get_ist!(456)
      ** (Ecto.NoResultsError)

  """
  def get_ist!(id) do
    Repo.get!(Ist, id)
  end
  @doc """
  Creates a ist.

  ## Examples

      iex> create_ist(%{field: value})
      {:ok, %Ist{}}

      iex> create_ist(%{field: bad_value})
      {:error, %Ecto.Changeset{}}

  """
  def create_ist(attrs \\ %{}) do
    %Ist{}
    |> Ist.changeset(attrs)
    |> Repo.insert()
  end

  @doc """
  Updates a ist.

  ## Examples

      iex> update_ist(ist, %{field: new_value})
      {:ok, %Ist{}}

      iex> update_ist(ist, %{field: bad_value})
      {:error, %Ecto.Changeset{}}

  """
  def update_ist(%Ist{} = ist, attrs) do
    ist
    |> Ist.changeset(attrs)
    |> Repo.update()
  end

  @doc """
  Deletes a Ist.

  ## Examples

      iex> delete_ist(ist)
      {:ok, %Ist{}}

      iex> delete_ist(ist)
      {:error, %Ecto.Changeset{}}

  """
  def delete_ist(%Ist{} = ist) do
    Repo.delete(ist)
  end

  @doc """
  Returns an `%Ecto.Changeset{}` for tracking ist changes.

  ## Examples

      iex> change_ist(ist)
      %Ecto.Changeset{source: %Ist{}}

  """
  def change_ist(%Ist{} = ist) do
    Ist.changeset(ist, %{})
  end

  alias Giom.Accounting.Point

  @doc """
  Returns the list of points.

  ## Examples

      iex> list_points()
      [%Point{}, ...]

  """
  def list_points do
    Repo.all(Point)
  end

  @doc """
  Gets a single point.

  Raises `Ecto.NoResultsError` if the Point does not exist.

  ## Examples

      iex> get_point!(123)
      %Point{}

      iex> get_point!(456)
      ** (Ecto.NoResultsError)

  """
  def get_point!(id), do: Repo.get!(Point, id)

  @doc """
  Creates a point.

  ## Examples

      iex> create_point(%{field: value})
      {:ok, %Point{}}

      iex> create_point(%{field: bad_value})
      {:error, %Ecto.Changeset{}}

  """
  def create_point(attrs \\ %{}) do
    %Point{}
    |> Point.changeset(attrs)
    |> Repo.insert()
  end

  @doc """
  Updates a point.

  ## Examples

      iex> update_point(point, %{field: new_value})
      {:ok, %Point{}}

      iex> update_point(point, %{field: bad_value})
      {:error, %Ecto.Changeset{}}

  """
  def update_point(%Point{} = point, attrs) do
    point
    |> Point.changeset(attrs)
    |> Repo.update()
  end

  @doc """
  Deletes a Point.

  ## Examples

      iex> delete_point(point)
      {:ok, %Point{}}

      iex> delete_point(point)
      {:error, %Ecto.Changeset{}}

  """
  def delete_point(%Point{} = point) do
    Repo.delete(point)
  end

  @doc """
  Returns an `%Ecto.Changeset{}` for tracking point changes.

  ## Examples

      iex> change_point(point)
      %Ecto.Changeset{source: %Point{}}

  """
  def change_point(%Point{} = point) do
    Point.changeset(point, %{})
  end

  alias Giom.Accounting.Stocktrans

  @doc """
  Returns the list of stocktrans.

  ## Examples

      iex> list_stocktrans()
      [%Stocktrans{}, ...]

  """
  def list_stocktrans do
    Repo.all(Stocktrans)
  end

  @doc """
  Gets a single stocktrans.

  Raises `Ecto.NoResultsError` if the Stocktrans does not exist.

  ## Examples

      iex> get_stocktrans!(123)
      %Stocktrans{}

      iex> get_stocktrans!(456)
      ** (Ecto.NoResultsError)

  """
  def get_stocktrans!(id), do: Repo.get!(Stocktrans, id)

  @doc """
  Creates a stocktrans.

  ## Examples

      iex> create_stocktrans(%{field: value})
      {:ok, %Stocktrans{}}

      iex> create_stocktrans(%{field: bad_value})
      {:error, %Ecto.Changeset{}}

  """
  def create_stocktrans(attrs \\ %{}) do
    %Stocktrans{}
    |> Stocktrans.changeset(attrs)
    |> Repo.insert()
  end

  @doc """
  Updates a stocktrans.

  ## Examples

      iex> update_stocktrans(stocktrans, %{field: new_value})
      {:ok, %Stocktrans{}}

      iex> update_stocktrans(stocktrans, %{field: bad_value})
      {:error, %Ecto.Changeset{}}

  """
  def update_stocktrans(%Stocktrans{} = stocktrans, attrs) do
    stocktrans
    |> Stocktrans.changeset(attrs)
    |> Repo.update()
  end

  @doc """
  Deletes a Stocktrans.

  ## Examples

      iex> delete_stocktrans(stocktrans)
      {:ok, %Stocktrans{}}

      iex> delete_stocktrans(stocktrans)
      {:error, %Ecto.Changeset{}}

  """
  def delete_stocktrans(%Stocktrans{} = stocktrans) do
    Repo.delete(stocktrans)
  end

  @doc """
  Returns an `%Ecto.Changeset{}` for tracking stocktrans changes.

  ## Examples

      iex> change_stocktrans(stocktrans)
      %Ecto.Changeset{source: %Stocktrans{}}

  """
  def change_stocktrans(%Stocktrans{} = stocktrans) do
    Stocktrans.changeset(stocktrans, %{})
  end

  alias Giom.Accounting.Ptrans

  @doc """
  Returns the list of ptrans.

  ## Examples

      iex> list_ptrans()
      [%Ptrans{}, ...]

  """
  def list_ptrans do
    Repo.all(Ptrans)
  end

  @doc """
  Gets a single ptrans.

  Raises `Ecto.NoResultsError` if the Ptrans does not exist.

  ## Examples

      iex> get_ptrans!(123)
      %Ptrans{}

      iex> get_ptrans!(456)
      ** (Ecto.NoResultsError)

  """
  def get_ptrans!(id), do: Repo.get!(Ptrans, id)

  @doc """
  Creates a ptrans.

  ## Examples

      iex> create_ptrans(%{field: value})
      {:ok, %Ptrans{}}

      iex> create_ptrans(%{field: bad_value})
      {:error, %Ecto.Changeset{}}

  """
  def create_ptrans(attrs \\ %{}) do
    %Ptrans{}
    |> Ptrans.changeset(attrs)
    |> Repo.insert()
  end

  @doc """
  Updates a ptrans.

  ## Examples

      iex> update_ptrans(ptrans, %{field: new_value})
      {:ok, %Ptrans{}}

      iex> update_ptrans(ptrans, %{field: bad_value})
      {:error, %Ecto.Changeset{}}

  """
  def update_ptrans(%Ptrans{} = ptrans, attrs) do
    ptrans
    |> Ptrans.changeset(attrs)
    |> Repo.update()
  end

  @doc """
  Deletes a Ptrans.

  ## Examples

      iex> delete_ptrans(ptrans)
      {:ok, %Ptrans{}}

      iex> delete_ptrans(ptrans)
      {:error, %Ecto.Changeset{}}

  """
  def delete_ptrans(%Ptrans{} = ptrans) do
    Repo.delete(ptrans)
  end

  @doc """
  Returns an `%Ecto.Changeset{}` for tracking ptrans changes.

  ## Examples

      iex> change_ptrans(ptrans)
      %Ecto.Changeset{source: %Ptrans{}}

  """
  def change_ptrans(%Ptrans{} = ptrans) do
    Ptrans.changeset(ptrans, %{})
  end
end
